package com.duan.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.duan.goods.mapper.ProductMapper;
import com.duan.goods.pojo.Product;
import com.duan.goods.pojo.ProductDetails;
import com.duan.goods.service.ProductDetailsService;
import com.duan.goods.mapper.ProductDetailsMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
* @author lenovo
* @description 针对表【goods_product_details(商品详情表)】的数据库操作Service实现
* @createDate 2024-07-30 11:54:42
*/
@Service
public class ProductDetailsServiceImpl extends ServiceImpl<ProductDetailsMapper, ProductDetails>
    implements ProductDetailsService{

    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<ProductDetails> selectAll() {
        LambdaQueryWrapper<ProductDetails> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductDetails::getIsDelete,0);
        return productDetailsMapper.selectList(queryWrapper);
    }

    @Override
    public List<ProductDetails> selectList(ProductDetails productDetails) {
        LambdaQueryWrapper<ProductDetails> queryWrapper=this.queryWrapper(productDetails);
        return productDetailsMapper.selectList(queryWrapper);
    }

    @Override
    public Integer insert(ProductDetails productDetails) {
        //商品规格最小价格当做商品价格
        //查询所有商品规格表
        LambdaQueryWrapper<ProductDetails> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductDetails::getIsDelete,0);
        queryWrapper.eq(ProductDetails::getProductId,productDetails.getProductId());
        queryWrapper.orderByAsc(ProductDetails::getPrice);
        List<ProductDetails> list = productDetailsMapper.selectList(queryWrapper);
        //判断有没有
        if(!list.isEmpty()){
            //判断谁是最小价格
            Product product=new Product();
            product.setId(productDetails.getProductId());
            if(productDetails.getPrice().compareTo(list.get(0).getPrice())>0){
                product.setPrice(list.get(0).getPrice());
            }else{
                product.setPrice(productDetails.getPrice());
            }
            productMapper.updateById(product);
        }
        return productDetailsMapper.insert(productDetails);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                ProductDetails productDetails = new ProductDetails();
                productDetails.setId(Long.parseLong(id));
                productDetails.setIsDelete(1);
                productDetailsMapper.updateById(productDetails);
            }
            return 1;
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public Integer update(ProductDetails productDetails) {
        return productDetailsMapper.updateById(productDetails);
    }

    @Override
    public Page<ProductDetails> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<ProductDetails>) this.selectAll();
    }

    @Override
    public Page<ProductDetails> pageList(Integer page, Integer size, ProductDetails productDetails) {
        PageHelper.startPage(page,size);
        return (Page<ProductDetails>) this.selectList(productDetails);
    }

    @Override
    public Integer reducedInventory(Long productId, Integer num) {
        int i = productDetailsMapper.reducedInventory(productId, num);
        return i;
    }

    public LambdaQueryWrapper<ProductDetails> queryWrapper(ProductDetails productDetails){
        LambdaQueryWrapper <ProductDetails> queryWrapper = new LambdaQueryWrapper<>();
        if (productDetails.getId() != null){
            queryWrapper.eq(ProductDetails::getId,productDetails.getId());
        }
        if (productDetails.getSpecId() != null){
            queryWrapper.eq(ProductDetails::getSpecId,productDetails.getSpecId());
        }
        if (productDetails.getNum() != null){
            queryWrapper.eq(ProductDetails::getNum,productDetails.getNum());
        }
        if (productDetails.getWeight() != null){
            queryWrapper.eq(ProductDetails::getWeight,productDetails.getWeight());
        }
        if (productDetails.getPrice() != null){
            queryWrapper.eq(ProductDetails::getPrice,productDetails.getPrice());
        }
        if (productDetails.getInventory() != null){
            queryWrapper.eq(ProductDetails::getInventory,productDetails.getInventory());
        }
        if (productDetails.getImg() != null){
            queryWrapper.eq(ProductDetails::getImg,productDetails.getImg());
        }
        if (productDetails.getProductId() != null){
            queryWrapper.eq(ProductDetails::getProductId,productDetails.getProductId());
        }
        if (productDetails.getIsDelete() != null){
            queryWrapper.eq(ProductDetails::getIsDelete,productDetails.getIsDelete());
        }
        if (productDetails.getCreateTime() != null){
            queryWrapper.eq(ProductDetails::getCreateTime,productDetails.getCreateTime());
        }
        if (productDetails.getUpdateTime() != null){
            queryWrapper.eq(ProductDetails::getUpdateTime,productDetails.getUpdateTime());
        }
        return queryWrapper;
    }
}




